<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="仓库名称" prop="warehouseName">
      <el-input v-model="dataForm.warehouseName" placeholder="仓库名称"></el-input>
    </el-form-item>
    <el-form-item label="仓库管理员" prop="warehouseAdmin">
      <el-input v-model="dataForm.warehouseAdmin" placeholder="仓库管理员"></el-input>
    </el-form-item>
    <el-form-item label="电话" prop="tel">
      <el-input v-model="dataForm.tel" placeholder="电话"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
			<el-select v-model="dataForm.status" placeholder="状态">
				<el-option label="正常" value="1"></el-option>
				<el-option label="禁用" value="2"></el-option>
			</el-select>
    </el-form-item>
    <el-form-item label="地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="地址"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :disabled="disabledSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
				disabledSubmit:false,
        dataForm: {
          id: 0,
          warehouseName: '',
          warehouseAdmin: '',
          sellerId: '',
          tel: '',
          status: '',
          address: '',
          remark: ''
        },
        dataRule: {
          warehouseName: [
            { required: true, message: '仓库名称不能为空', trigger: 'blur' }
          ],
          warehouseAdmin: [
            { required: true, message: '仓库管理员不能为空', trigger: 'blur' }
          ],
          tel: [
            { required: true, message: '电话不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/store/tstorewarehouse/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.warehouseName = data.tStoreWarehouse.warehouseName
                this.dataForm.warehouseAdmin = data.tStoreWarehouse.warehouseAdmin
                this.dataForm.sellerId = data.tStoreWarehouse.sellerId
                this.dataForm.tel = data.tStoreWarehouse.tel
                this.dataForm.status = data.tStoreWarehouse.status
                this.dataForm.address = data.tStoreWarehouse.address
                this.dataForm.remark = data.tStoreWarehouse.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
				this.disabledSubmit = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/store/tstorewarehouse/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'warehouseName': this.dataForm.warehouseName,
                'warehouseAdmin': this.dataForm.warehouseAdmin,
                'sellerId': this.dataForm.sellerId,
                'tel': this.dataForm.tel,
                'status': this.dataForm.status,
                'address': this.dataForm.address,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
										this.disabledSubmitv = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
          else {
            this.disabledSubmit = false
          }
        })
      }
    }
  }
</script>
